// let url = "http://localhost:8082/login_msg"
// let xhr = new XMLHttpRequest()
// xhr.open('get', url)
// xhr.send()
// xhr.onreadystatechange = function() {
//     // let obj = JSON.parse(xhr.response)
//     // console.log(obj)
//     console.log(xhr.responseText)
// }


const username = document.querySelector('[name=username]')

// 搜索框
const input1 = document.querySelector('.mi input')
const ul2 = document.querySelector('.result-list')
input1.addEventListener('focus', function () {
	ul2.style.display = 'block'
	input1.classList.add('search')
})
input1.addEventListener('blur', function () {
	ul2.style.display = 'none'
	input1.classList.remove('search')
})

//发送验证码

//手机号验证
const phone = document.querySelector('[name=phone]')
phone.addEventListener('change', verifyPhone)

function verifyPhone() {
	const span = phone.nextElementSibling
	const reg = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-36-9])\d{8}$/
	if (!reg.test(phone.value)) {
		span.innerHTML = '输入不合法，请输入11为手机号'
		return false
	}
	span.innerHTML = ''
	return true
}

//密码验证
const password = document.querySelector('[name=password]')
password.addEventListener('change', verifypassword)

function verifypassword() {
	const span = password.nextElementSibling
	const reg = /^(?![a-zA-Z]+$)(?!\d+$)(?![^\da-zA-Z\s]+$).{6,20}$/
	if (!reg.test(password.value)) {
		span.innerHTML = '6-20位,至少包含字母、数字、特殊字符'
		return false
	}
	span.innerHTML = ''
	return true
}
//确认密码验证
const confirm = document.querySelector('[name=confirm]')
confirm.addEventListener('change', verifyconfirm)

function verifyconfirm() {
	const span = confirm.nextElementSibling
	if (confirm.value !== password.value) {
		span.innerHTML = '两次密码输入不一致'
		return false
	}
	span.innerHTML = ''
	return true
}

//我同意验证
const queren = document.querySelector('.icon-queren')
queren.addEventListener('click', function () {
	this.classList.toggle('icon-queren2')
})


// 生成随机验证码
var arrStr = ['O', 'P', 'Q', 'R', 'S', 'T', '2', '3', '4', '5', '6', '7', '8'];
var t2 = document.querySelector('#tq2');
var submits = document.querySelector('.xtx-card form').lastElementChild;
var spanShow = document.querySelector('#mjdf');
var vc = '';
console.log(spanShow, 'sas')
window.onload = function ()//用window的onload事件，窗体加载完毕的时候
{
	for (i = 0; i < 4; i++) {
		var myRNum = Math.floor(Math.random() * arrStr.length);
		vc = vc + arrStr[myRNum]
	}
	spanShow.innerHTML = vc;
}

//提交模块
const form = document.querySelector('.submit')
//点击下一步时要执行的函数
form.addEventListener('click', function (e) {
	if (!queren.classList.contains('icon-queren2')) {
		alert('请勾选用户协议')
		e.preventDefault()
		return
	}
	else if (!verifyPhone()) {
		alert('请检查你的手机号！')
		e.preventDefault()
		return
	} else if (!(t2.value != null && t2.value == vc)) {
		alert("验证码未获取或输入错误，不能提交！")
		return
	}
	else if (!verifypassword()) {
		alert('请检查你的密码！')
		e.preventDefault()
		return

	}
	else if (!verifyconfirm()) {
		alert('请检查你的密码！')
		e.preventDefault()
		return
	}

	var data = { "username": username.value, "userphone": phone.value, "userpassword": password.value }

	// submits.addEventListener("click",function(){
	// 	if(!(t2.value != null && t2.value == vc) ) {
	// 		alert("验证码未获取或输入错误，不能提交！")
	// 		return
	// 	}
	// })
	$.ajax({
		type: "post",  //数据提交方式（post/get）
		url: "http://127.0.0.1:8088/api/register",  //提交到的url
		data: data,//提交的数据
		dataType: "json",//返回的数据类型格式
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		xhrFields: {
			withCredentials: true // 携带跨域cookie
		},
		success: function (msg) {
			if (msg.status == 200) {  //注册成功
				console.log("注册成功！正在为您跳转登录页面......");
				document.querySelector('.tankuang').style.display = 'block',
					document.querySelector('#layer_msg').style.color = 'green'
				$('#layer_msg').text('注册成功！正在为您跳转登录页面......')
				setTimeout(function () {
					document.querySelector('.tankuang').style.display = 'none'
					location.href = '../login.html'
				}, 1000)
			} else if (msg.status == 403) {  //注册失败
				console.log(msg.message);
				document.querySelector('.tankuang').style.display = 'block',
					document.querySelector('#layer_msg').style.color = 'red'
				$('#layer_msg').text(msg.message)
				setTimeout(function () {
					document.querySelector('.tankuang').style.display = 'none'
				}, 1500)
			} else if (msg.status == 402) {  //注册失败
				console.log(msg.message);
				document.querySelector('.tankuang').style.display = 'block',
					document.querySelector('#layer_msg').style.color = 'red'
				$('#layer_msg').text(msg.message)
				setTimeout(function () {
					document.querySelector('.tankuang').style.display = 'none'
				}, 1500)
			} else {
				console.log("注册失败，您可以发送邮件至2722354955@qq.com！");
				document.querySelector('.tankuang').style.display = 'block',
					document.querySelector('#layer_msg').style.color = 'red'
				$('#layer_msg').text('注册失败，您可以发送邮件至2722354955@qq.com！')
				setTimeout(function () {
					document.querySelector('.tankuang').style.display = 'none'
				}, 3500)
			}
		}
	});

})




